import sys, os

import cldt_datamanip as dm

if __name__ == '__main__':
    postfix = ""
    if len(sys.argv) == 2:
        postfix = sys.argv[1]
    
    variationNames = [ "nomacro", "uniqueness", "cohesion", "domain" ]
    
    titles = [ "variation", 
        "non-d. cover", "diag. cover", "tot. cover",
        "non-d. add.", "diag. add.", "tot. add",
        "non-d. missed", "diag. missed", "tot. missed",
        "non-d. excl. +", "diag. excl. +", "tot. excl. +",
        "non-d. excl. -", "diag. excl. -", "tot. excl. -" ]
    
    coverTable = dict()
    
    prevFileTable = None
    for vn in variationNames:
        dataFile = "a%s.%s.cldt" % ( postfix, vn )
        
        def nop(*a): pass
        
        fileTable = []
        def fileSink(fileID, filePath, length, checksum):
            fileTable.append(( fileID, filePath ))
        coverExtractCloneSink = dm.CoverExtractCloneSink()
        
        f = open(dataFile, "r")
        dm.scan_clone_data_file(f, nop, fileSink, coverExtractCloneSink)
        f.close()
        
        if prevFileTable is not None:
            assert fileTable == prevFileTable
        
        coverTable[vn] = coverExtractCloneSink.cloneCoveringFilePairSet
    
    output = open("comparisonresult%s.txt" % postfix, "w")
    
    def to_nondiagonal(coverPairSet): return sorted(filter(dm.is_nondiagonal, coverPairSet))
    def to_diagonal(coverPairSet): return sorted(filter(dm.is_diagonal, coverPairSet))
    def to_total(coverPairSet): return sorted(coverPairSet)
    def to_nondiagonal_diagonal(coverPairSet):
        return to_nondiagonal(coverPairSet), to_diagonal(coverPairSet), to_total(coverPairSet)
    output.write("%s\n" % "\t".join(titles))
    for vi, vn in enumerate(variationNames):
        nd_cvr, di_cvr, tot_cvr = to_nondiagonal_diagonal(coverTable[vn])
        
        additional = coverTable[vn] - coverTable["nomacro"]
        nd_additional, di_additional, tot_additional = to_nondiagonal_diagonal(additional)
        
        missed = coverTable["nomacro"] - coverTable[vn]
        nd_missed, di_missed, tot_missed = to_nondiagonal_diagonal(missed)
        
        exclusivePositive = set()
        exclusivePositive.update(coverTable[vn])
        for wi, wn in enumerate(variationNames):
            if wi == vi: continue
            exclusivePositive.difference_update(coverTable[wn])
        nd_ex_pos_cvr, di_ex_pos_cvr, tot_ex_pos_cvr = to_nondiagonal_diagonal(exclusivePositive)
        
        exclusiveNegative = set()
        for wi, wn in enumerate(variationNames):
            if wi == vi: continue
            exclusiveNegative.union(coverTable[wn])
        exclusiveNegative.difference_update(coverTable[vn])
        nd_ex_neg_cvr, di_ex_neg_cvr, tot_ex_neg_cvr = to_nondiagonal_diagonal(exclusiveNegative)
        
        output.write("%s\t%s\n" % ( vn, "\t".join("%d" % len(cvr) for cvr in [ 
                nd_cvr, di_cvr, tot_cvr,
                nd_additional, di_additional, tot_additional,
                nd_missed, di_missed, tot_missed,
                nd_ex_pos_cvr, di_ex_pos_cvr, tot_ex_pos_cvr,
                nd_ex_neg_cvr, di_ex_neg_cvr, tot_ex_neg_cvr ]) ))
    
    output.write("\n")
    
    covers = coverTable.values()
    print covers
    cmn = covers[0]
    for cvr in covers[1:]:
        cmn.intersection_update(cvr)
    
    di_common = sorted(filter(dm.is_nondiagonal, cmn))
    nd_common = sorted(filter(dm.is_diagonal, cmn))
    
    output.write("%s\n" % "\t".join(["", "non-d. common", "diag. common", "tot. common"]))
    output.write("%s\n" % "\t".join("%d" % v for v in (nd_common, di_common, len(cmn))))
